package com.tang.lts.core.factory;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Tang
 * @classname NameThreadFactory
 * @description [  ]
 * @date 2022/4/28 16:29
 */
public class NameThreadFactory implements ThreadFactory {

    private static final AtomicInteger POOL_SQL = new AtomicInteger(1);

    private final AtomicInteger threadNum = new AtomicInteger(1);

    private final String prefix;

    private final boolean daemon;

    public NameThreadFactory(String prefix) {
        this(prefix + "-thread-", false);
    }

    public NameThreadFactory(String prefix, boolean daemon) {
        this.prefix = prefix;
        this.daemon = daemon;
    }

    @Override
    public Thread newThread(Runnable r) {
        String name = prefix + threadNum.getAndIncrement();
        Thread thread = new Thread(r,name);
        thread.setDaemon(daemon);
        return thread;
    }

}